package cn.datawin.routeserver.input.service;

import cn.datawin.constant.ApiStatus;
import cn.datawin.sms.cmpp2.ReportLog;
import cn.datawin.sms.cmpp2.SmsDetailLog;
import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.common.message.Message;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static cn.datawin.constant.RocketMqConstant.SMS_CMPP_REPORT_NOTIFY_TOPIC;

@Slf4j
@Service
@RequiredArgsConstructor
public class ErrorReportService {

	private final ProduceService produceService;

	public void reportError(ApiStatus apiStatus, SmsDetailLog smsDetailLog) {
		Long id = smsDetailLog.getId();
		//获取短信日志的crMsgId
		List<String> split = Arrays.stream(smsDetailLog.getCrMsgId().split(",")).toList();
		for (String s : split) {
			ReportLog reportLog = new ReportLog();
			reportLog.setSmsDetailLogId(id);
			reportLog.setCreateDate(new Date());
			String status = apiStatus.getStatus();
			reportLog.setStat("ERR:"+status);
			reportLog.setPhoneNo(smsDetailLog.getPhoneNos());
			reportLog.setCstmCustomerCode(smsDetailLog.getCstmCustomerCode());
			reportLog.setCstmCustomerId(smsDetailLog.getCstmCustomerId());
			reportLog.setCrMsgId(s);
			String t = DateUtil.format(new Date(), "yyMMddHHmmss");
			reportLog.setSubmitTime(t);
			reportLog.setDoneTime(t);
			String ymd = DateUtil.format(new Date(), "yyyyMMdd");
			reportLog.setYmd(ymd);

			//发送短信状态报告
			produceService.sendReportMessage(reportLog,smsDetailLog.getAccessServerTag());
		}



	}
}
